<template>
  <div class="vue3-cron-plus-container">
    <el-tabs type="border-card">
      <!-- 秒、分、时、天、月、年标签页内容保持不变 -->
      <!-- 分标签页（内容不变） -->
      <el-tab-pane>
        <template #label>
          <span><el-icon><Calendar /></el-icon> {{ state.text.Minutes.name }}</span>
        </template>
        <div class="tabBody myScroller">
          <!-- 分部分内容不变 -->
          <el-row>
            <el-radio v-model="state.minute.cronEvery" label="1">{{ state.text.Minutes.every }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.minute.cronEvery" label="2"
              >{{ state.text.Minutes.interval[0] }}
              <el-input-number v-model="state.minute.incrementIncrement" :min="1" :max="60" />
              {{ state.text.Minutes.interval[1] }}
              <el-input-number v-model="state.minute.incrementStart" :min="0" :max="59" />
              {{ state.text.Minutes.interval[2] || "" }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio class="long" v-model="state.minute.cronEvery" label="3"
              >{{ state.text.Minutes.specific }}
              <el-select multiple v-model="state.minute.specificSpecific">
                <el-option v-for="(val, index) in 60" :key="index" :value="val - 1" >{{ val - 1 }}</el-option>
              </el-select>
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.minute.cronEvery" label="4"
              >{{ state.text.Minutes.cycle[0] }}
              <el-input-number v-model="state.minute.rangeStart" :min="1" :max="60" />
              {{ state.text.Minutes.cycle[1] }}
              <el-input-number v-model="state.minute.rangeEnd" :min="0" :max="59" />
              {{ state.text.Minutes.cycle[2] }}
            </el-radio>
          </el-row>
        </div>
      </el-tab-pane>

      <!-- 时、天、月、年标签页内容保持不变 -->
      <el-tab-pane>
        <template #label>
          <span><el-icon><Calendar /></el-icon> {{ state.text.Hours.name }}</span>
        </template>
        <div class="tabBody myScroller">
          <!-- 时部分内容不变 -->
          <el-row>
            <el-radio v-model="state.hour.cronEvery" label="1">{{ state.text.Hours.every }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.hour.cronEvery" label="2"
              >{{ state.text.Hours.interval[0] }}
              <el-input-number v-model="state.hour.incrementIncrement" :min="0" :max="23" />
              {{ state.text.Hours.interval[1] }}
              <el-input-number v-model="state.hour.incrementStart" :min="0" :max="23" />
              {{ state.text.Hours.interval[2] }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio class="long" v-model="state.hour.cronEvery" label="3"
              >{{ state.text.Hours.specific }}
              <el-select multiple v-model="state.hour.specificSpecific">
                <el-option v-for="(val, index) in 24" :key="index" :value="val - 1" >{{ val - 1 }}</el-option>
              </el-select>
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.hour.cronEvery" label="4"
              >{{ state.text.Hours.cycle[0] }}
              <el-input-number v-model="state.hour.rangeStart" :min="0" :max="23" />
              {{ state.text.Hours.cycle[1] }}
              <el-input-number v-model="state.hour.rangeEnd" :min="0" :max="23" />
              {{ state.text.Hours.cycle[2] }}
            </el-radio>
          </el-row>
        </div>
      </el-tab-pane>

      <el-tab-pane>
        <template #label>
          <span><el-icon><Calendar /></el-icon> {{ state.text.Day.name }}</span>
        </template>
        <div class="tabBody myScroller">
          <!-- 天部分内容不变 -->
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="1">{{ state.text.Day.every }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="2"
              >{{ state.text.Day.intervalWeek[0] }}
              <el-input-number v-model="state.week.incrementIncrement" :min="1" :max="7" />
              {{ state.text.Day.intervalWeek[1] }}
              <el-select v-model="state.week.incrementStart">
                <el-option v-for="(val, index) in 7" :key="index" :label="state.text.Week[val - 1]" :value="val" />
              </el-select>
              {{ state.text.Day.intervalWeek[2] }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="3"
              >{{ state.text.Day.intervalDay[0] }}
              <el-input-number v-model="state.day.incrementIncrement" :min="1" :max="31" />
              {{ state.text.Day.intervalDay[1] }}
              <el-input-number v-model="state.day.incrementStart" :min="1" :max="31" />
              {{ state.text.Day.intervalDay[2] }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio class="long" v-model="state.day.cronEvery" label="4"
              >{{ state.text.Day.specificWeek }}
              <el-select multiple v-model="state.week.specificSpecific">
                <el-option v-for="(val, index) in 7" :key="index" :label="state.text.Week[val - 1]"
                  :value="['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'][val - 1]" />
              </el-select>
            </el-radio>
          </el-row>
          <el-row>
            <el-radio class="long" v-model="state.day.cronEvery" label="5"
              >{{ state.text.Day.specificDay }}
              <el-select multiple v-model="state.day.specificSpecific">
                <el-option v-for="(val, index) in 31" :key="index" :value="val" >{{ val }}</el-option>
              </el-select>
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="6">{{ state.text.Day.lastDay }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="7">{{ state.text.Day.lastWeekday }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="8"
              >{{ state.text.Day.lastWeek[0] }}
              <el-select v-model="state.day.cronLastSpecificDomDay">
                <el-option v-for="(val, index) in 7" :key="index" :label="state.text.Week[val - 1]" :value="val" />
              </el-select>
              {{ state.text.Day.lastWeek[1] || "" }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="9">
              <el-input-number v-model="state.day.cronDaysBeforeEomMinus" :min="1" :max="31" />
              {{ state.text.Day.beforeEndMonth[0] }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="10"
              >{{ state.text.Day.nearestWeekday[0] }}
              <el-input-number v-model="state.day.cronDaysNearestWeekday" :min="1" :max="31" />
              {{ state.text.Day.nearestWeekday[1] }}
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.day.cronEvery" label="11"
              >{{ state.text.Day.someWeekday[0] }}
              <el-input-number v-model="state.week.cronNthDayNth" :min="1" :max="5" />
              <el-select v-model="state.week.cronNthDayDay">
                <el-option v-for="(val, index) in 7" :key="index" :label="state.text.Week[val - 1]" :value="val" />
              </el-select>
              {{ state.text.Day.someWeekday[1] }}
            </el-radio>
          </el-row>
        </div>
      </el-tab-pane>

      <el-tab-pane>
        <template #label>
          <span><el-icon><Calendar /></el-icon> {{ state.text.Month.name }}</span>
        </template>
        <div class="tabBody myScroller">
          <!-- 月部分内容不变 -->
          <el-row>
            <el-radio v-model="state.month.cronEvery" label="1">{{ state.text.Month.every }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.month.cronEvery" label="2"
              >{{ state.text.Month.interval[0] }}
              <el-input-number v-model="state.month.incrementIncrement" :min="0" :max="12" />
              {{ state.text.Month.interval[1] }}
              <el-input-number v-model="state.month.incrementStart" :min="0" :max="12" />
            </el-radio>
          </el-row>
          <el-row>
            <el-radio class="long" v-model="state.month.cronEvery" label="3"
              >{{ state.text.Month.specific }}
              <el-select multiple v-model="state.month.specificSpecific">
                <el-option v-for="(val, index) in 12" :key="index" :label="val" :value="val" />
              </el-select>
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.month.cronEvery" label="4"
              >{{ state.text.Month.cycle[0] }}
              <el-input-number v-model="state.month.rangeStart" :min="1" :max="12" />
              {{ state.text.Month.cycle[1] }}
              <el-input-number v-model="state.month.rangeEnd" :min="1" :max="12" />
            </el-radio>
          </el-row>
        </div>
      </el-tab-pane>

      <el-tab-pane>
        <template #label>
          <span><el-icon><Calendar /></el-icon> {{ state.text.Year.name }}</span>
        </template>
        <div class="tabBody myScroller">
          <!-- 年部分内容不变 -->
          <el-row>
            <el-radio v-model="state.year.cronEvery" label="1">{{ state.text.Year.every }}</el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.year.cronEvery" label="2"
              >{{ state.text.Year.interval[0] }}
              <el-input-number v-model="state.year.incrementIncrement" :min="1" :max="99" />
              {{ state.text.Year.interval[1] }}
              <el-input-number v-model="state.year.incrementStart" :min="2018" :max="2118" />
            </el-radio>
          </el-row>
          <el-row>
            <el-radio class="long" v-model="state.year.cronEvery" label="3"
              >{{ state.text.Year.specific }}
              <el-select filterable multiple v-model="state.year.specificSpecific">
                <el-option v-for="(val, index) in 100" :key="index" :label="2017 + val" :value="2017 + val" />
              </el-select>
            </el-radio>
          </el-row>
          <el-row>
            <el-radio v-model="state.year.cronEvery" label="4"
              >{{ state.text.Year.cycle[0] }}
              <el-input-number v-model="state.year.rangeStart" :min="2018" :max="2118" />
              {{ state.text.Year.cycle[1] }}
              <el-input-number v-model="state.year.rangeEnd" :min="2018" :max="2118" />
            </el-radio>
          </el-row>
        </div>
      </el-tab-pane>
    </el-tabs>
    {{ state.cron }}
  </div>
</template>

<script lang="ts" setup>
import { Language, type LanguageType } from "./language";
import { computed, reactive, toRefs, type PropType } from "vue";
import { Calendar } from '@element-plus/icons-vue';

type i18nType = "cn" | "en" | "pt";
const props = defineProps({
  i18n: {
    type: String as PropType<i18nType>,
    default: "cn",
  },
});
const { i18n } = toRefs(props);
const state = reactive({
  // 各时间部分的状态定义（保持不变）
  second: {
    cronEvery: "1",
    incrementStart: 3,
    incrementIncrement: 5,
    rangeStart: 0,
    rangeEnd: 0,
    specificSpecific: [],
  },
  minute: {
    cronEvery: "1",
    incrementStart: 3,
    incrementIncrement: 5,
    rangeStart: 0,
    rangeEnd: 0,
    specificSpecific: [],
  },
  hour: {
    cronEvery: "1",
    incrementStart: 3,
    incrementIncrement: 5,
    rangeStart: 0,
    rangeEnd: 0,
    specificSpecific: [],
  },
  day: {
    cronEvery: "1",
    incrementStart: 1,
    incrementIncrement: 1,
    rangeStart: 0,
    rangeEnd: 0,
    specificSpecific: [],
    cronLastSpecificDomDay: 1,
    cronDaysBeforeEomMinus: 0,
    cronDaysNearestWeekday: 0,
  } as {
    cronEvery: string;
    incrementStart: number;
    incrementIncrement: number;
    rangeStart: number;
    rangeEnd: number;
    specificSpecific: any[];
    cronLastSpecificDomDay: number;
    cronDaysBeforeEomMinus: number;
    cronDaysNearestWeekday: number;
  },
  week: {
    cronEvery: "1",
    incrementStart: 1,
    incrementIncrement: 1,
    specificSpecific: [],
    cronNthDayDay: 1,
    cronNthDayNth: 1,
  } as {
    cronEvery: string;
    incrementStart: number;
    incrementIncrement: number;
    specificSpecific: any[];
    cronNthDayDay: number;
    cronNthDayNth: number;
  },
  month: {
    cronEvery: "1",
    incrementStart: 3,
    incrementIncrement: 5,
    rangeStart: 0,
    rangeEnd: 0,
    specificSpecific: [],
  },
  year: {
    cronEvery: "1",
    incrementStart: 2017,
    incrementIncrement: 1,
    rangeStart: 0,
    rangeEnd: 0,
    specificSpecific: [],
  } as {
    cronEvery: string;
    incrementStart: number;
    incrementIncrement: number;
    rangeStart: number;
    rangeEnd: number;
    specificSpecific: any[];
  },
  text: computed<LanguageType>(() => Language[i18n.value]),

  // 核心：判断是否只修改了某个部分（其他部分保持默认）
  isOnlyMinuteChanged: computed(() => {
    return state.minute.cronEvery !== "1" &&  // 分钟被修改
      state.second.cronEvery === "1" &&       // 其他部分未修改
      state.hour.cronEvery === "1" &&
      state.day.cronEvery === "1" &&
      state.month.cronEvery === "1" &&
      state.year.cronEvery === "1";
  }),

  isOnlyHourChanged: computed(() => {
    return state.hour.cronEvery !== "1" &&    // 小时被修改
      state.second.cronEvery === "1" &&       // 其他部分未修改
      state.minute.cronEvery === "1" &&
      state.day.cronEvery === "1" &&
      state.month.cronEvery === "1" &&
      state.year.cronEvery === "1";
  }),

  isOnlyDayChanged: computed(() => {
    return state.day.cronEvery !== "1" &&     // 天被修改
      state.second.cronEvery === "1" &&       // 其他部分未修改
      state.minute.cronEvery === "1" &&
      state.hour.cronEvery === "1" &&
      state.month.cronEvery === "1" &&
      state.year.cronEvery === "1";
  }),

  isOnlyMonthChanged: computed(() => {
    return state.month.cronEvery !== "1" &&   // 月被修改
      state.second.cronEvery === "1" &&       // 其他部分未修改
      state.minute.cronEvery === "1" &&
      state.hour.cronEvery === "1" &&
      state.day.cronEvery === "1" &&
      state.year.cronEvery === "1";
  }),

  isOnlyYearChanged: computed(() => {
    return state.year.cronEvery !== "1" &&    // 年被修改
      state.second.cronEvery === "1" &&       // 其他部分未修改
      state.minute.cronEvery === "1" &&
      state.hour.cronEvery === "1" &&
      state.day.cronEvery === "1" &&
      state.month.cronEvery === "1";
  }),

  // 秒文本计算（核心修改）
  secondsText: computed(() => {
    let seconds = "";
    const cronEvery = state.second.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
        // 以下情况需要将秒的*替换为0：
        // 1. 只改分钟（分钟前只有秒）
        // 2. 只改小时（小时前有秒、分）
        // 3. 只改天（天前有秒、分、时）
        // 4. 只改月（月前有秒、分、时、天）
        // 5. 只改年（年前有秒、分、时、天、月）
        if (state.isOnlyMinuteChanged || state.isOnlyHourChanged || 
            state.isOnlyDayChanged || state.isOnlyMonthChanged || state.isOnlyYearChanged) {
          seconds = "0";
        } else {
          seconds = "*";
        }
        break;
      case "2":
        seconds = state.second.incrementStart + "/" + state.second.incrementIncrement;
        break;
      case "3":
        state.second.specificSpecific.map((val) => {
          seconds += val + ",";
        });
        seconds = seconds.slice(0, -1) || "";
        break;
      case "4":
        seconds = state.second.rangeStart + "-" + state.second.rangeEnd;
        break;
    }
    return seconds;
  }),

  // 分文本计算（核心修改）
  minutesText: computed(() => {
    let minutes = "";
    const cronEvery = state.minute.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
        // 以下情况需要将分的*替换为0：
        // 1. 只改小时（小时前有秒、分）
        // 2. 只改天（天前有秒、分、时）
        // 3. 只改月（月前有秒、分、时、天）
        // 4. 只改年（年前有秒、分、时、天、月）
        if (state.isOnlyHourChanged || state.isOnlyDayChanged || 
            state.isOnlyMonthChanged || state.isOnlyYearChanged) {
          minutes = "0";
        } else {
          minutes = "*";
        }
        break;
      case "2":
        minutes = state.minute.incrementStart + "/" + state.minute.incrementIncrement;
        break;
      case "3":
        state.minute.specificSpecific.map((val) => {
          minutes += val + ",";
        });
        minutes = minutes.slice(0, -1) || "";
        break;
      case "4":
        minutes = state.minute.rangeStart + "-" + state.minute.rangeEnd;
        break;
    }
    return minutes;
  }),

  // 时文本计算（核心修改）
  hoursText: computed(() => {
    let hours = "";
    const cronEvery = state.hour.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
        // 以下情况需要将时的*替换为0：
        // 1. 只改天（天前有秒、分、时）
        // 2. 只改月（月前有秒、分、时、天）
        // 3. 只改年（年前有秒、分、时、天、月）
        if (state.isOnlyDayChanged || state.isOnlyMonthChanged || state.isOnlyYearChanged) {
          hours = "0";
        } else {
          hours = "*";
        }
        break;
      case "2":
        hours = state.hour.incrementStart + "/" + state.hour.incrementIncrement;
        break;
      case "3":
        state.hour.specificSpecific.map((val) => {
          hours += val + ",";
        });
        hours = hours.slice(0, -1) || "";
        break;
      case "4":
        hours = state.hour.rangeStart + "-" + state.hour.rangeEnd;
        break;
    }
    return hours;
  }),

  // 天文本计算（核心修改）
  daysText: computed(() => {
    let days = "";
    const cronEvery = state.day.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
        // 以下情况需要将天的*替换为0：
        // 1. 只改月（月前有秒、分、时、天）
        // 2. 只改年（年前有秒、分、时、天、月）
        if (state.isOnlyMonthChanged || state.isOnlyYearChanged) {
          days = "0";
        } else {
          days = "*"; // 原逻辑中case 1返回空，这里修正为*保持一致性
        }
        break;
      case "2":
      case "4":
      case "11":
        days = "?";
        break;
      case "3":
        days = state.day.incrementStart + "/" + state.day.incrementIncrement;
        break;
      case "5":
        state.day.specificSpecific.map((val) => {
          days += val + ",";
        });
        days = days.slice(0, -1) || "";
        break;
      case "6":
        days = "L";
        break;
      case "7":
        days = "LW";
        break;
      case "8":
        days = state.day.cronLastSpecificDomDay + "L";
        break;
      case "9":
        days = "L-" + state.day.cronDaysBeforeEomMinus;
        break;
      case "10":
        days = state.day.cronDaysNearestWeekday + "W";
        break;
    }
    return days;
  }),

  // 周文本计算（保持不变）
  weeksText: computed(() => {
    let weeks = "";
    const cronEvery = state.day.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
      case "3":
      case "5":
        weeks = "?";
        break;
      case "2":
        weeks = state.week.incrementStart + "/" + state.week.incrementIncrement;
        break;
      case "4":
        state.week.specificSpecific.map((val) => {
          weeks += val + ",";
        });
        weeks = weeks.slice(0, -1) || "";
        break;
      case "6":
      case "7":
      case "8":
      case "9":
      case "10":
        weeks = "?";
        break;
      case "11":
        weeks = state.week.cronNthDayDay + "#" + state.week.cronNthDayNth;
        break;
    }
    return weeks;
  }),

  // 月文本计算（核心修改）
  monthsText: computed(() => {
    let months = "";
    const cronEvery = state.month.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
        // 只改年时，月的*需要替换为0（年前包含月）
        if (state.isOnlyYearChanged) {
          months = "0";
        } else {
          months = "*";
        }
        break;
      case "2":
        months = state.month.incrementStart + "/" + state.month.incrementIncrement;
        break;
      case "3":
        state.month.specificSpecific.map((val) => {
          months += val + ",";
        });
        months = months.slice(0, -1) || "";
        break;
      case "4":
        months = state.month.rangeStart + "-" + state.month.rangeEnd;
        break;
    }
    return months;
  }),

  // 年文本计算（保持不变）
  yearsText: computed(() => {
    let years = "";
    const cronEvery = state.year.cronEvery;
    switch (cronEvery.toString()) {
      case "1":
        years = "*";
        break;
      case "2":
        years = state.year.incrementStart + "/" + state.year.incrementIncrement;
        break;
      case "3":
        state.year.specificSpecific.map((val) => {
          years += val + ",";
        });
        years = years.slice(0, -1) || "";
        break;
      case "4":
        years = state.year.rangeStart + "-" + state.year.rangeEnd;
        break;
    }
    return years;
  }),

  // 最终Cron表达式（保持不变）
  cron: computed((): string => {
    return `${state.secondsText || "*"} ${state.minutesText || "*"} ${
      state.hoursText || "*"
    } ${state.daysText || "*"} ${state.monthsText || "*"} ${
      state.weeksText || "?"
    } ${state.yearsText || "*"}`;
  }),
});

// 以下方法保持不变
const getValue = () => {
  return state.cron;
};
let lastError = "";
const setValue = (cronStr: string): boolean => {
  lastError = "";
  try {
    if (!cronStr || typeof cronStr !== "string") throw new Error("empty");
    const parts = cronStr.trim().split(/\s+/);
    if (parts.length < 6 || parts.length > 7) throw new Error("len");
    const [sec, min, hour, day, month, week, year = "*"] = parts;

    const toNum = (v: string) => {
      const n = Number(v);
      if (Number.isNaN(n)) throw new Error("NaN");
      return n;
    };
    const parseSimpleField = (
      part: string,
      target:
        | typeof state.second
        | typeof state.minute
        | typeof state.hour
        | typeof state.month
        | typeof state.year,
      allowZeroStart = true
    ) => {
      target.cronEvery = "1";
      target.incrementStart = allowZeroStart ? 0 : 1;
      target.incrementIncrement = 1;
      target.rangeStart = 0;
      target.rangeEnd = 0;
      // @ts-ignore
      target.specificSpecific = [];

      if (part === "*") {
        target.cronEvery = "1";
        return;
      }
      if (part.includes("/")) {
        const [s, i] = part.split("/");
        target.cronEvery = "2";
        // @ts-ignore
        target.incrementStart = toNum(s);
        // @ts-ignore
        target.incrementIncrement = toNum(i);
        return;
      }
      if (part.includes(",")) {
        target.cronEvery = "3";
        // @ts-ignore
        target.specificSpecific = part.split(",").map((v) => toNum(v));
        return;
      }
      if (part.includes("-")) {
        target.cronEvery = "4";
        const [s, e] = part.split("-");
        // @ts-ignore
        target.rangeStart = toNum(s);
        // @ts-ignore
        target.rangeEnd = toNum(e);
        return;
      }
      target.cronEvery = "3";
      // @ts-ignore
      target.specificSpecific = [toNum(part)];
    };

    parseSimpleField(sec, state.second, true);
    parseSimpleField(min, state.minute, true);
    parseSimpleField(hour, state.hour, true);
    parseSimpleField(month, state.month, false);
    parseSimpleField(year, state.year, false);

    state.day.cronEvery = "1";
    state.day.incrementStart = 1;
    state.day.incrementIncrement = 1;
    state.day.specificSpecific = [];
    state.day.cronLastSpecificDomDay = 1;
    state.day.cronDaysBeforeEomMinus = 0;
    state.day.cronDaysNearestWeekday = 0;

    state.week.incrementStart = 1;
    state.week.incrementIncrement = 1;
    state.week.specificSpecific = [];
    state.week.cronNthDayDay = 1;
    state.week.cronNthDayNth = 1;

    const isWeekUnset = (w: string) => w === "?";
    const isDayUnset = (d: string) => d === "?";

    const parseDay = (d: string) => {
      if (d === "*") {
        state.day.cronEvery = "1";
        return;
      }
      if (d === "?") {
        return;
      }
      if (d === "L") {
        state.day.cronEvery = "6";
        return;
      }
      if (d === "LW") {
        state.day.cronEvery = "7";
        return;
      }
      if (/^\d+L$/.test(d)) {
        state.day.cronEvery = "8";
        state.day.cronLastSpecificDomDay = toNum(d.slice(0, -1));
        return;
      }
      if (/^L-\d+$/.test(d)) {
        state.day.cronEvery = "9";
        state.day.cronDaysBeforeEomMinus = toNum(d.slice(2));
        return;
      }
      if (/^\d+W$/.test(d)) {
        state.day.cronEvery = "10";
        state.day.cronDaysNearestWeekday = toNum(d.slice(0, -1));
        return;
      }
      if (d.includes("/")) {
        state.day.cronEvery = "3";
        const [s, i] = d.split("/");
        state.day.incrementStart = toNum(s);
        state.day.incrementIncrement = toNum(i);
        return;
      }
      if (d.includes(",")) {
        state.day.cronEvery = "5";
        state.day.specificSpecific = d.split(",").map((v) => toNum(v));
        return;
      }
      state.day.cronEvery = "5";
      state.day.specificSpecific = [toNum(d)];
    };

    const parseWeek = (w: string) => {
      if (w === "?") {
        return;
      }
      if (w.includes("/")) {
        state.day.cronEvery = "2";
        const [s, i] = w.split("/");
        state.week.incrementStart = toNum(s);
        state.week.incrementIncrement = toNum(i);
        return;
      }
      if (w.includes("#")) {
        state.day.cronEvery = "11";
        const [dayOfWeek, nth] = w.split("#");
        state.week.cronNthDayDay = toNum(dayOfWeek);
        state.week.cronNthDayNth = toNum(nth);
        return;
      }
      const toWeekStr = (v: string) => v.trim();
      if (w.includes(",")) {
        state.day.cronEvery = "4";
        state.week.specificSpecific = w.split(",").map(toWeekStr);
        return;
      }
      state.day.cronEvery = "4";
      state.week.specificSpecific = [toWeekStr(w)];
    };

    parseDay(day);
    if (isDayUnset(day)) {
      parseWeek(week);
    }

    if (isDayUnset(day) && isWeekUnset(week)) throw new Error("both?");

    return true;
  } catch (e: any) {
    const mapError = (code: string) => {
      switch (code) {
        case "empty":
          return "表达式为空";
        case "len":
          return `字段数量应为6或7个`;
        case "NaN":
          return "存在需为数字的字段无法转换为数字";
        case "both?":
          return `"日"和"星期"字段不能同时为 '?'`;
        default:
          return "不支持或格式错误";
      }
    };
    const code = typeof e?.message === "string" ? e.message : "unknown";
    console.log(code);
    lastError = mapError(code);
    return false;
  }
};
const getLastError = () => lastError;
defineExpose({
  getValue,
  setValue,
  getLastError,
});
</script>

<style lang="scss" scoped>
.vue3-cron-plus-container {
  .el-row {
    margin-bottom: 10px;
  }
  .tabBody {
    padding: 10px;
  }
  .long {
    display: flex;
    align-items: center;
    gap: 8px;
  }
  .myScroller {
    max-height: 400px;
    overflow-y: auto;
  }
}
</style>